// pages/home/editPost/editPost.js
import {
  http
} from '../../../utils/apiutils'
import {
  input
} from '../../../utils/tools'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    jobId: '',
    factoryId: '',
    checked: true,
    popShow: false,
    form: {},
    result: [],
    // currentDate: new Date().getTime(),
    currentDate: '12:00',
    currentDate2: '13:00',
    formatter(type, value) {
      if (type === 'hour') {
        return `${value}时`;
      }
      if (type === 'minute') {
        return `${value}分`;
      }
      return value;
    },
    dateKey: '',
    dateKey2: '',
    startAM: '',
    endAM: '',
    startPM: '',
    endPM: '',
    dateshow: false,
    dateshow2: false,
    range: [],
    actAddr: 0,
    state: false,
    dateArr:[],


  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      jobId: options.jobId,
      factoryId: options.factoryId
    })
    this.getJobDetail(options.jobId)
    this.getAddress({
      factoryId: options.factoryId
    })


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  // 获取岗位详情
  getJobDetail(id) {
    http.getJobData(id, res => {
      if (res.code == 0) {
        if (res.data.interviewTime&&res.data.interviewTime!=null) {
          let a = res.data.interviewTime.replace(/\//g, '').replace(/\[/g, '').replace(/\]/g, '').replace(/\"/g, '').replace(/\"/g, '').replace(/\s+/g, '').split(',')
          this.setData({
            startAM: (a[0].split('-'))[0].substring(0, 5),
            endAM: (a[0].split('-'))[1].substring(0, 5),
            startPM: (a[1].split('-'))[0].substring(0, 5),
            endPM: (a[1].split('-'))[1].substring(0, 5),
          })
        }else{
          res.data.interviewTime=''
        }
        if (res.data.interviewDate&&res.data.interviewDate!='[]') {
          let b = res.data.interviewDate.replace(/\//g, '').replace(/\[/g, '').replace(/\]/g, '').replace(/\"/g, '').replace(/\"/g, '').replace(/\s+/g, '').split(',')
          this.setData({
            result: b,
          })
        }else{
          res.data.interviewDate=''
        }
        this.setData({
          form: res.data,
          state: res.data.status == 2 ? true : false,
        })
      } else {
        wx.showToast({
          title: res.msg || res.message,
          icon: 'none'
        })
      }
    }, err => {})
  },
  // 获取地址列表
  getAddress(params) {
    http.getAddress(params, res => {
      if (res.status == 200) {
        this.setData({
          range: res.obj,
          actAddr: 0
        })
      } else {
        wx.showToast({
          title: res.msg || res.message,
          icon: 'none'
        })
      }
    }, err => {})
  },
  input(event) {
    input.bind(this)(event)
  },
  onSwitchChange({
    detail
  }) {
    // 需要手动对 checked 状态进行更新
    this.setData({
      state: detail
    });
  },
  addAddress() {
    let _this = this;
    wx.chooseLocation({
      success(res) {
        _this.setData({
          ['form.lat']: res.latitude,
          ['form.lng']: res.longitude,
          ['form.workPlace']: res.name + ' ' + res.address
        })
      },
      fail: function (err) {
        console.log('err', err)
      }
    })
  },
  onPopClose() {
    this.setData({
      popShow: false
    })
  },
  chooseTime() {
    this.setData({
      popShow: true
    })
  },
  onCheckboxChange(e) {
    this.setData({
      result: e.detail,
      // ['form.interviewDate']: e.detail
    })
  },
  showDate(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      dateKey: key,
      dateshow: true
    })
  },
  showDate2(e) {
    let key = e.currentTarget.dataset.key;
    this.setData({
      dateKey2: key,
      dateshow2: true
    })
  },
  onInput(value) {
    this.setData({
      // ["queryParams." + this.data.dateKey]: formatTime(value.detail),
      [this.data.dateKey]: value.detail,
      dateshow: false,
      dateKey: ''
    })
    // let arr = []
    let am = this.data.startAM + '-' + this.data.endAM
    let pm = this.data.startPM + '-' + this.data.endPM
    // arr.push(am)
    // arr.push(pm)
    this.data.dateArr.push(am)
    this.data.dateArr.push(pm)
    // this.setData({
    //   ['form.interviewTime']: arr
    // })
  },
  onInput2(value) {
    this.setData({
      // ["queryParams." + this.data.dateKey]: formatTime(value.detail),
      [this.data.dateKey2]: value.detail,
      dateshow2: false,
      dateKey2: '',
    })
    // let arr = []
    let am = this.data.startAM + '-' + this.data.endAM
    let pm = this.data.startPM + '-' + this.data.endPM
    // arr.push(am)
    // arr.push(pm)
    this.data.dateArr.push(am)
    this.data.dateArr.push(pm)
    // this.setData({
    //   ['form.interviewTime']: arr
    // })
  },
  cancel() {
    this.setData({
      dateKey: '',
      dateshow: false
    })
  },
  cancel2() {
    this.setData({
      dateKey2: '',
      dateshow2: false
    })
  },
  addrPcikerchange(e) {
    this.setData({
      actAddr: e.detail.value,
      ['form.lat']: this.data.range[e.detail.value].lat,
      ['form.lng']: this.data.range[e.detail.value].lng,
      ['form.workPlace']: this.data.range[e.detail.value].address
    })
  },
  clickSubmit() {
    let params = {}
    params.id = this.data.jobId - 0
    params.interviewDate = []
    this.data.result.map(item => {
      params.interviewDate.push(item)
    })
    params.interviewTime = []
    let am = this.data.startAM + ':00' + '-' + this.data.endAM + ':00'
    let pm = this.data.startPM + ':00' + '-' + this.data.endPM + ':00'
    params.interviewTime.push(am)
    params.interviewTime.push(pm)
    params.contactPerson = this.data.form.contactPerson
    params.contactPhone = this.data.form.contactPhone
    params.workPlace = this.data.form.workPlace
    params.notice = this.data.form.notice
    params.lat = this.data.form.lat
    params.lng = this.data.form.lng
    if (this.data.state) {
      params.status = 2
    } else {
      params.status = 10
    }
    http.changeJob(params, res => {
      if (res.status == 200) {
        wx.navigateBack({
          delta: 1,
        })
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
    }, err => {})

  },
  // 取消
  clickQx(){
    this.setData({
      popShow:false
    })
    this.getJobDetail(this.data.jobId)
  },
  // 确定
  clickQd(){
    this.setData({
      ['form.interviewDate']: this.data.result,
      ['form.interviewTime']: this.data.dateArr,
      popShow:false
    })
  }
})